/*******************************************************************************
   @file    dev_IuEab.c
   @author  H.M.Shim (elecage@gmail.com)
   @version 
   @date    2013. 7. 31.
   @brief   
  ******************************************************************************
  The MIT License (MIT)

Copyright (c) 2013 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  ******************************************************************************
  */



/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "usbstdio.h"
#include "devio.h"
#include "dev_gpio.h"
#include "dev_i2c.h"
#include "dev_spi.h"
#include "dev_IuEab.h"
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_it.h"
#include "cmdline.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define DEV_IUEAB1_DEVICE_NAME ((int8_t*)"iueab1")	/**< @brief IU-EAB 1번 보드 장치명 */
#define DEV_IUEAB2_DEVICE_NAME ((int8_t*)"iueab2")	/**< @brief IU-EAB 2번 보드 장치명 */
#define DEV_IUEAB3_DEVICE_NAME ((int8_t*)"iueab3")	/**< @brief IU-EAB 3번 보드 장치명 */
#define READ_SIZE 19
#define ADC_OFFSET 32768
/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
Dev_IuEab_flag devIuEabFlag[NUM_EAB];				/**< @brief 각 GPIO 별 상태 flag */

int8_t *devIuEabName[NUM_EAB] = {DEV_IUEAB1_DEVICE_NAME,
									  DEV_IUEAB2_DEVICE_NAME,
									  DEV_IUEAB3_DEVICE_NAME};
void DefaultCallback(int32_t* data);
AdcCallback callback = DefaultCallback;
/* Private function prototypes -----------------------------------------------*/
void dev_IuEab1Dready_ISR(void);
void dev_IuEab2Dready_ISR(void);
void dev_IuEab3Dready_ISR(void);
void resetAdc(int16_t dd);
void eabDelay(int16_t delay);
/* Exported functions ------------------------------------------------------- */
int32_t registerDev_IuEab(void)
{
	int i;
		DeviceStructure devStructure;
		for(i = 0 ; i < NUM_EAB ; i++) {
			devStructure.dd 			= Dev_EAB1 + i;
			devStructure.devname 		= devIuEabName[i];
			devStructure.openFunc 		= dev_IuEabOpen;
			devStructure.readFunc 		= dev_IuEabRead;
			devStructure.writeFunc 		= dev_IuEabWrite;
			devStructure.ioctlFunc 		= dev_IuEabIoctl;
			devStructure.closeFunc 		= dev_IuEabClose;
	#ifdef USE_CMDLINE
			devStructure.openCmdFunc 	= dev_IuEabOpenCmd;
			devStructure.readCmdFunc	= dev_IuEabReadCmd;
			devStructure.writeCmdFunc	= dev_IuEabWriteCmd;
			devStructure.ioctlCmdFunc	= dev_IuEabIoctlCmd;
			devStructure.closeCmdFunc	= dev_IuEabCloseCmd;
	#endif /* USE_CMDLINE */

			devIuEabFlag[i].stat = 0;

			registerDevice(devStructure);
		}

		return 0;
}
int32_t dev_IuEabInit(int32_t dd)
{
	return 0;
}

int32_t dev_IuEabOpen(const int8_t* devname, int32_t flags)
{
	int32_t devNum;

	GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_Config extiConfig;
	if (strcmp((const char*) devname, (const char*) DEV_IUEAB1_DEVICE_NAME)
			== 0) {
		devNum = 0;
	} else if (strcmp((const char*) devname, (const char*) DEV_IUEAB1_DEVICE_NAME)
			== 0) {
		devNum = 1;
	} else if (strcmp((const char*) devname, (const char*) DEV_IUEAB1_DEVICE_NAME)
			== 0) {
		devNum = 2;
	}

	switch(devNum) {
	case 0:
		devIuEabFlag[devNum].i2c_dd = devOpen(DEV_IU_EAB1_I2C_NAME, DEV_RDWR | DEV_FLAG_I2C_7BITS_ADDRESS | DEV_FLAG_MAP_SIZE_1);
		// TODO i2c1에 대해 정리할 것
		// SPI configuration
		devIuEabFlag[devNum].spi_dd = devOpen(DEV_IU_EAB1_SPI_NAME, DEV_RDWR | DEV_IU_EAB_SPI_FLAG_POL |
				DEV_IU_EAB_SPI_FLAG_PHA | DEV_IU_EAB_SPI_FLAG_FIRST_BIT);


		int32_t flags = DEV_RDWR |  DEV_IU_EAB1_GPIO_POWER_EN_PIN	|
				DEV_IU_EAB1_GPIO_ADC_PWDN_PIN | DEV_IU_EAB1_GPIO_ADC_RESET_PIN |
				DEV_IU_EAB1_GPIO_ADC_START_PIN;
		devIuEabFlag[devNum].gpio_dd = devOpen(DEV_IU_EAB1_GPIO_NAME, flags);

		// GPIO configuration
		uint16_t gpiobit = DEV_IU_EAB1_GPIO_ADC_START_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_CLEAR_BIT, &gpiobit);
		gpiobit = DEV_IU_EAB1_GPIO_POWER_EN_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
		eabDelay(100);
		gpiobit = DEV_IU_EAB1_GPIO_ADC_PWDN_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
		eabDelay(100);
		resetAdc(devNum + Dev_EAB1);
		eabDelay(100);

		GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_IN;
		GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP;
		GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_NOPULL;
		GPIO_InitStructure.GPIO_Pin 	= DEV_IU_EAB1_GPIO_ADC_DRDY_PIN;
		GPIO_InitStructure.GPIO_Speed  	= GPIO_Speed_50MHz;

		GPIO_Init(DEV_IU_EAB_GPIO_PORT, &GPIO_InitStructure);

		extiConfig.func = dev_IuEab1Dready_ISR;
		extiConfig.mode = EXTI_Mode_Interrupt;
		extiConfig.pinsource = DEV_IU_EAB1_GPIO_ADC_PIN_SOURCE;
		extiConfig.priority = 0x08;
		extiConfig.subPriority = 0x00;
		extiConfig.trigger = EXTI_Trigger_Falling;

		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_CONFIG_INTERRUPT, &extiConfig);
		break;
	case 1:
		// TODO 2번 보드에 대해 정리할 것
		break;
	case 2:
		// TODO 3번 보드에 대해 정리할 것
		break;
	}
	devIuEabFlag[devNum].stat |= DEV_STAT_OPEN;
	return Dev_EAB1 + devNum; /**< Device Descriptor를 리턴 */
}
ssize_t dev_IuEabWrite(int32_t dd, const void *buf, size_t n)
{
	int32_t devNum = dd - Dev_EAB1;
	if((devIuEabFlag[devNum].stat & DEV_STAT_OPEN ) == 0)
		return DEV_ERR_NOT_OPEN;
	return 0;
}
ssize_t	dev_IuEabRead(int32_t dd, void *buf, size_t nbytes)
{
	int32_t devNum = dd - Dev_EAB1;
	if((devIuEabFlag[devNum].stat & DEV_STAT_OPEN ) == 0)
		return DEV_ERR_NOT_OPEN;
	return 0;
}
ssize_t dev_IuEabIoctl(int32_t dd, int32_t request, void* data)
{
	int32_t devNum = dd - Dev_EAB1;
	uint16_t gpiobit;
	SPI_CMD_DATA_U cmdData;
	uint8_t writedata[10];
	uint8_t opcode;

	uint32_t d;
	if((devIuEabFlag[devNum].stat & DEV_STAT_OPEN ) == 0)
		return DEV_ERR_NOT_OPEN;

	switch(request) {
	case DEV_IU_EAB_START:
		/*
		 gpiobit = DEV_IU_EAB1_GPIO_ADC_START_PIN;
		 devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
		 */
		opcode = 0x08;
		devWrite(devIuEabFlag[devNum].spi_dd, &opcode, 1);
		break;
	case DEV_IU_EAB_STOP:
		opcode = 0x0A;
		devWrite(devIuEabFlag[devNum].spi_dd, &opcode, 1);
		/*
		gpiobit = DEV_IU_EAB1_GPIO_ADC_START_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_CLEAR_BIT, &gpiobit);
		*/
		break;
	case DEV_IU_EAB_ADC_POWER_DOWN:
		gpiobit = DEV_IU_EAB1_GPIO_ADC_PWDN_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_CLEAR_BIT, &gpiobit);
		break;
	case DEV_IU_EAB_ADC_POWER_EN:
		gpiobit = DEV_IU_EAB1_GPIO_ADC_PWDN_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
		break;
	case DEV_IU_EAB_POWER_DOWN:
		gpiobit = DEV_IU_EAB1_GPIO_POWER_EN_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_CLEAR_BIT, &gpiobit);
		break;
	case DEV_IU_EAB_POWER_EN:
		gpiobit = DEV_IU_EAB1_GPIO_POWER_EN_PIN;
		devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
		break;
	case DEV_IU_EAB_RESET:
		resetAdc(dd);
		break;
	case DEV_IU_EAB_SET_CONFIG1:
		cmdData.s.cmd = ADC_CMD_WREG | ADC_REG_CONFIG1;
		cmdData.s.receivedLength = 0;
		cmdData.s.null = 0;
		devWrite(devIuEabFlag[devNum].spi_dd, cmdData.c, sizeof(cmdData));
		*(uint8_t*)data = SPI_ReceiveData(DEV_IU_EAB1_SPI_PORT);

		break;
	case DEV_IU_EAB_SET_CONFIG2:
		break;
	case DEV_IU_EAB_SET_CONFIG3:
		break;
	case DEV_IU_EAB_SET_CONFIG4:
		break;
	case DEV_IU_EAB_SET_CHANNEL:
		break;
	case DEV_IU_EAB_GET_CONFIG1:
		cmdData.s.cmd = ADC_CMD_WREG | ADC_REG_CONFIG1;
		cmdData.s.receivedLength = 0;
		cmdData.s.null = 0;
		devWrite(devIuEabFlag[devNum].spi_dd, cmdData.c, sizeof(cmdData));
		devIuEabFlag[devNum].adc_Reg_config1 =  SPI_ReceiveData(DEV_IU_EAB1_SPI_PORT);
		*(uint8_t*)data = devIuEabFlag[devNum].adc_Reg_config1;

		break;
	case DEV_IU_EAB_GET_CONFIG2:
		cmdData.s.cmd = ADC_CMD_WREG | ADC_REG_CONFIG2;
		cmdData.s.receivedLength = 0;
		cmdData.s.null = 0;
		devWrite(devIuEabFlag[devNum].spi_dd, cmdData.c, sizeof(cmdData));
		devIuEabFlag[devNum].adc_Reg_config2 =  SPI_ReceiveData(DEV_IU_EAB1_SPI_PORT);
		*(uint8_t*)data = devIuEabFlag[devNum].adc_Reg_config2;
		break;
	case DEV_IU_EAB_GET_CONFIG3:
		cmdData.s.cmd = ADC_CMD_WREG | ADC_REG_CONFIG3;
		cmdData.s.receivedLength = 0;
		cmdData.s.null = 0;
		devWrite(devIuEabFlag[devNum].spi_dd, cmdData.c, sizeof(cmdData));
		devIuEabFlag[devNum].adc_Reg_config3 =  SPI_ReceiveData(DEV_IU_EAB1_SPI_PORT);
		*(uint8_t*)data = devIuEabFlag[devNum].adc_Reg_config3;
		break;
	case DEV_IU_EAB_GET_CONFIG4:
		cmdData.s.cmd = ADC_CMD_WREG | ADC_REG_CONFIG4;
		cmdData.s.receivedLength = 0;
		cmdData.s.null = 0;
		devWrite(devIuEabFlag[devNum].spi_dd, cmdData.c, sizeof(cmdData));
		devIuEabFlag[devNum].adc_Reg_config4 =  SPI_ReceiveData(DEV_IU_EAB1_SPI_PORT);
		*(uint8_t*)data = devIuEabFlag[devNum].adc_Reg_config4;
		break;
	case DEV_IU_EAB_GET_CHANNEL:
		break;
	case DEV_IU_EAB_SET_DAISY_EN:
		break;
	case DEV_IU_EAB_SET_DAISY_DIS:
		break;
	case DEV_IU_EAB_SET_INT_OSC:
		break;
	case DEV_IU_EAB_SET_EXT_OSC:
		break;
	case DEV_IU_EAB_SET_SR:
		d = *(int32_t*)data;
		switch(d) {
		case 125:
			writedata[2] = 0x26;
			break;
		case 250:
			writedata[2] = 0x25;
			break;
		case 500:
			writedata[2] = 0x24;
			break;
		case 1000:
			writedata[2] = 0x23;
			break;
		case 2000:
			writedata[2] = 0x22;
			break;
		case 4000:
			writedata[2] = 0x21;
			break;
		case 8000:
			writedata[2] = 0x20;
			break;
		}
		writedata[0] = 0x01 | 0x40;
		writedata[1] = 0x00;

		dev_SpiWrite(Dev_SPI2, writedata, 3);
		break;
	case DEV_IU_EAB_SET_CH_PD:
		break;
	case DEV_IU_EAB_SET_CH_GAIN:
		d = *(int8_t*)data;
		switch(d) {
		case 1:
			writedata[2] = 0x10;
			writedata[3] = 0x10;
			writedata[4] = 0x10;
			writedata[5] = 0x10;
			writedata[6] = 0x10;
			writedata[7] = 0x10;
			writedata[8] = 0x10;
			writedata[9] = 0x10;
			break;
		case 2:
			writedata[2] = 0x20;
			writedata[3] = 0x20;
			writedata[4] = 0x20;
			writedata[5] = 0x20;
			writedata[6] = 0x20;
			writedata[7] = 0x20;
			writedata[8] = 0x20;
			writedata[9] = 0x20;
			break;
		case 3:
			writedata[2] = 0x30;
			writedata[3] = 0x30;
			writedata[4] = 0x30;
			writedata[5] = 0x30;
			writedata[6] = 0x30;
			writedata[7] = 0x30;
			writedata[8] = 0x30;
			writedata[9] = 0x30;
			break;
		case 4:
			writedata[2] = 0x40;
			writedata[3] = 0x40;
			writedata[4] = 0x40;
			writedata[5] = 0x40;
			writedata[6] = 0x40;
			writedata[7] = 0x40;
			writedata[8] = 0x40;
			writedata[9] = 0x40;
			break;
		case 6:
			writedata[2] = 0x00;
			writedata[3] = 0x00;
			writedata[4] = 0x00;
			writedata[5] = 0x00;
			writedata[6] = 0x00;
			writedata[7] = 0x00;
			writedata[8] = 0x00;
			writedata[9] = 0x00;
			break;
		case 8:
			writedata[2] = 0x50;
			writedata[3] = 0x50;
			writedata[4] = 0x50;
			writedata[5] = 0x50;
			writedata[6] = 0x50;
			writedata[7] = 0x50;
			writedata[8] = 0x50;
			writedata[9] = 0x50;
			break;
		case 12:
			writedata[2] = 0x60;
			writedata[3] = 0x60;
			writedata[4] = 0x60;
			writedata[5] = 0x60;
			writedata[6] = 0x60;
			writedata[7] = 0x60;
			writedata[8] = 0x60;
			writedata[9] = 0x60;
			break;
		}
		writedata[0] = 0x05 | 0x40;
		writedata[1] = 0x03;

		dev_SpiWrite(Dev_SPI2, writedata, 10);
		break;
	case DEV_IU_EAB_SET_CH_MUX:
		break;
	case DEV_IU_EAB_GET_CH_PD:
		break;
	case DEV_IU_EAB_GET_CH_GAIN:
		break;
	case DEV_IU_EAB_GET_CH_MUX:
		break;
	case DEV_IU_EAB_GET_ID:
		writedata[0] = 0x20;
		writedata[1] = 0x01;
		writedata[2] = 0x00;
		writedata[3] = 0x00;
		dev_SpiWrite(Dev_SPI2, writedata, 4);


		usbPrintf((int8_t*)"ID = %2x %2x %2x %2x\n", writedata[0], writedata[1], writedata[2], writedata[3]);
	}

	return 0;
}


int32_t dev_IuEabClose(int32_t dd)
{
	int32_t devNum = dd - Dev_EAB1;
	if((devIuEabFlag[devNum].stat & DEV_STAT_OPEN ) == 0)
		return DEV_ERR_NOT_OPEN;
	return 0;
}

void dev_IuEabConfigEXTI(uint8_t portsource, EXTI_Config* config)
{
}


void dev_IuEab1Dready_ISR(void) {
	int8_t readData[READ_SIZE] = {0, };
	int32_t ch[8] = {0,};
	int16_t temp;

	dev_SpiRead(Dev_SPI2, readData, READ_SIZE);
	ch[0] = (int32_t)readData[3];
	ch[0] <<= 8;
	temp = (int32_t)((int16_t)readData[4] & 0x00ff);
	ch[0] |= (temp & 0x00ff);

	ch[1] = (int32_t)readData[5];
	ch[1] *= 256;
	temp = (int32_t)((int16_t)readData[6] & 0x00ff);
	ch[1] |= (temp & 0x00ff);

	ch[2] = (int32_t)readData[7];
	ch[2] *= 256;
	temp = (int32_t)((int16_t)readData[8] & 0x00ff);
	ch[2] |= (temp & 0x00ff);

	ch[3] = (int32_t)readData[9];
	ch[3] *= 256;
	temp = (int32_t)((int16_t)readData[10] & 0x00ff);
	ch[3] |= (temp & 0x00ff);

	(*callback)(ch);
#ifdef USE_KPU_BOARD
#else
		//	usbPrintf((int8_t*)"%d\n", ch[0]+ADC_OFFSET);
#endif /* USE KPU_BOARD */
//	dev_IueabDefalutCallback(ch);
				/*
			ch1 = (uint16_t)readData[3];
			ch1 <<= 8;
			ch1 |= (uint32_t)readData[4];
			if(ch1 < 16384)
				ch1 += 16384;
			else
				ch1 -= 16384;

			//ch1 = (((uint16_t)readData[3])<<8)  | ((uint16_t)readData[4]);
			usbPrintf("%u\n", ch1);
			*/
}
void dev_IuEab2Dready_ISR(void) {

}
void dev_IuEab3Dready_ISR(void)
{

}

#ifdef USE_CMDLINE

int32_t dev_IuEabOpenCmd(int32_t argc, int8_t *argv[])
{
	int32_t flags = 0;
		int32_t k = 0;
		int32_t err = 0;
		if(argc < 3)
			return CMDLINE_TOO_FEW_ARGS;
		DeviceStructure* device = deviceTable;

		while(device->devname != 0) {
			if(strcmp((char*)device->devname, (char*)argv[1]) == 0) {

				for(k = 2 ; k < argc ; k++) {

					if(strcmp((char*)argv[k], "readonly")  == 0) {
						flags |= DEV_RDONLY;
					}
					if(strcmp((char*)argv[k], "writeonly") == 0) {
						flags |= DEV_WRONLY;
					}
					if(strcmp((char*)argv[k], "readwrite") == 0) {
						flags |= DEV_RDWR;
					}

				}
				err = dev_IuEabOpen(device->devname, flags);
				if(err < 0) {
					usbPrintf((int8_t*)"error occurred.\n");
					return CMDLINE_INVALID_ARGS;
				} else {
					usbPrintf((int8_t*)"Device %s is opened.\n ", device->devname);
					return 0;
				}
			}
			device++;
		}
		return CMDLINE_INVALID_ARGS;
}
int32_t dev_IuEabReadCmd(int32_t argc, int8_t *argv[])
{
	return 0;
}
int32_t dev_IuEabWriteCmd(int32_t argc, int8_t *argv[])
{

	return 0;
}
int32_t dev_IuEabIoctlCmd(int32_t argc, int8_t *argv[])
{
	//int32_t flags = 0;
	//int32_t k = 0;
	int32_t err = 0;
	int32_t data;
	if(argc < 3)
		return CMDLINE_TOO_FEW_ARGS;
	DeviceStructure* device = deviceTable;

	while(device->devname != 0) {
		if(strcmp((char*)device->devname, (char*)argv[1]) == 0) {

			if(strcmp((char*)argv[2], DEV_IU_EAB_STOP_CMD) == 0) {
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_STOP, 0 );
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_START_CMD) == 0) {

				err =  dev_IuEabIoctl(device->dd, DEV_IU_EAB_START, 0 );
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_ADC_POWER_DOWN_CMD) == 0) {
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_ADC_POWER_DOWN, 0 );
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_ADC_POWER_EN_CMD) == 0) {
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_ADC_POWER_EN, 0 );
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_POWER_DOWN_CMD) == 0) {
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_POWER_DOWN, 0 );
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_POWER_EN_CMD) == 0) {
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_POWER_EN, 0 );
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_SET_GAIN_CMD) == 0) {
				if(argc< 4)
					return CMDLINE_TOO_FEW_ARGS;
				data = (int32_t)atoi((char*)argv[3]);
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_SET_CH_GAIN, &data);
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_SET_SR_CMD) == 0) {
				if(argc< 4)
					return CMDLINE_TOO_FEW_ARGS;
				data = (int32_t)atoi((char*)argv[3]);
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_SET_SR, &data);
			}
			if(strcmp((char*)argv[2], DEV_IU_EAB_ID_CMD) == 0) {
				err = dev_IuEabIoctl(device->dd, DEV_IU_EAB_GET_ID, 0);
			}
			return err;
		}


		device++;
	}
	return CMDLINE_INVALID_ARGS;
}
int32_t dev_IuEabCloseCmd(int32_t argc, int8_t *argv[])
{
	return 0;
}

#endif /* USE_CMDLINE */


void resetAdc(int16_t dd)
{
	int16_t devNum = dd - Dev_EAB1;
	uint16_t gpiobit ;
	switch(devNum)
	{
	case 0:
		gpiobit = DEV_IU_EAB1_GPIO_ADC_RESET_PIN;
		break;
	case 1:
		break;
	case 2:
		break;
	}
	devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
	eabDelay(100);
	devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_CLEAR_BIT, &gpiobit);
	eabDelay(1000);
	devIoctl(devIuEabFlag[devNum].gpio_dd, DEV_GPIO_SET_BIT, &gpiobit);
	eabDelay(100);

}

void eabDelay(int16_t delay)
{
	while(delay--);
}

void DefaultCallback(int32_t* data)
{
	int i;
	uint8_t packet[16];
	uint8_t temp;

	/*
	data[0] += ADC_OFFSET;
	data[1] += ADC_OFFSET;
	data[2] += ADC_OFFSET;
	data[3] += ADC_OFFSET;
	*/
	temp = (uint8_t)((data[0] >> 12) & 0xff) & 0x0f;
	packet[0] = temp;
	temp = (uint8_t)((data[0] >> 8) & 0xff) & 0x0f;
	packet[1] = temp | 0x10;
	temp = (uint8_t)((data[0] >> 4) & 0xff) & 0x0f;
	packet[2] = temp | 0x20;
	temp = (uint8_t)(data[0] & 0xff) & 0x0f;
	packet[3] = temp | 0x30;

	temp = (uint8_t)((data[1] >> 12) & 0xff) & 0x0f;
	packet[4] = temp | 0x40;
	temp = (uint8_t)((data[1] >> 8) & 0xff) & 0x0f;
	packet[5] = temp | 0x50;
	temp = (uint8_t)((data[1] >> 4) & 0xff) & 0x0f;
	packet[6] = temp | 0x60;
	temp = (uint8_t)(data[1] & 0xff) & 0x0f;
	packet[7] = temp | 0x70;

	temp = (uint8_t)((data[2] >> 12) & 0xff) & 0x0f;
	packet[8] = temp | 0x80;
	temp = (uint8_t)((data[2] >> 8) & 0xff) & 0x0f;
	packet[9] = temp | 0x90;
	temp = (uint8_t)((data[2] >> 4) & 0xff) & 0x0f;
	packet[10] = temp | 0xa0;
	temp = (uint8_t)(data[2] & 0xff) & 0x0f;
	packet[11] = temp | 0xb0;

	temp = (uint8_t)((data[3] >> 12) & 0xff) & 0x0f;
	packet[12] = temp | 0xc0;
	temp = (uint8_t)((data[3] >> 8) & 0xff) & 0x0f;
	packet[13] = temp | 0xd0;
	temp = (uint8_t)((data[3] >> 4) & 0xff) & 0x0f;
	packet[14] = temp | 0xe0;
	temp = (uint8_t)(data[3] & 0xff) & 0x0f;
	packet[15] = temp | 0xf0;

	usbWrite(packet, 16);

}
